package com.mall.goods.controller;

import com.github.pagehelper.PageInfo;
import com.mall.entity.Result;
import com.mall.entity.StatusCode;
import com.mall.goods.service.BrandService;
import com.mall.pojo.Brand;
import com.mall.pojo.Spec;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: bamboo_shoot on 20/5/20
 * @description: -
 */
@RestController
@RequestMapping("brand")
@CrossOrigin
public class BrandController {
    @Autowired
    BrandService brandService;


    @GetMapping
    public Result<Brand> findAll() {
        List<Brand> brandList = brandService.findAll();
        return new Result<Brand>(true, StatusCode.OK, "查询成功", brandList);
    }

    @GetMapping("/{id}")
    public Result<Brand> findBrandById(@PathVariable Integer id) {
        Brand brand = brandService.findBrandById(id);
        return new Result<>(true, StatusCode.OK, "根据id查询成功", brand);
    }

    @PostMapping
    public Result<Brand> add(@RequestBody Brand brand) {
        brandService.add(brand);
        return new Result<>(true, StatusCode.OK, "新增成功");
    }

    /**
     * 修改品牌
     * 这里应该通过id找到具体的要修改的brand
     *
     * @param: [brand]
     * @return: com.mall.entity.Result<com.mall.pojo.Brand>
     */
    @PutMapping("/{id}")
    public Result<Brand> updateBrand(@RequestBody Brand brand, @PathVariable Integer id) {
//        这里要根据id找到对应的brand
        brand.setId(id);
//        修改数据
        brandService.updateBrand(brand);
        return new Result<>(true, StatusCode.OK, "修改成功");
    }

    @DeleteMapping("/{id}")
    public Result<Brand> deleteBrandById(@PathVariable Integer id) {
        brandService.deleteBrandById(id);
        return new Result<>(true, StatusCode.OK, "删除成功");
    }

    @GetMapping("search")
    public Result<Brand> findBrandListByCondition(Brand brand) {
        List<Brand> brands = brandService.findBrandListByCondition(brand);
        return new Result<>(true, StatusCode.OK, "根据条件查询成功", brands);
    }

    @GetMapping("search/{page}/{size}")
    public Result<Brand> searchBrandsByPagination(Brand brand, @PathVariable int page, @PathVariable int size) {
        PageInfo<Brand> brandPageInfo = brandService.searchBrandsByPagination(brand, page, size);
        return new Result<>(true, StatusCode.OK, "分页查询成功", brandPageInfo);
    }

    @GetMapping("category/{category}")
    public Result<Brand> searchBrandByCategoryName(@PathVariable String category) {
        List<Brand> brands = brandService.searchBrandByCategoryName(category);
        return new Result<>(true, StatusCode.OK, "根据商品分类名称查询品牌列表成功", brands);
    }

    @GetMapping("spec/{category}")
    public Result<Spec> searchSpecificationByCategoryName(@PathVariable String category) {
        List<Spec> specs = brandService.searchSpecificationByCategoryName(category);
        return new Result<>(true, StatusCode.OK, "根据商品分类名称查询规格列表成功", specs);
    }

}
